//
//  AddViewController.m
//  网络记事本
//
//  Created by wukeng on 16/4/2.
//  Copyright © 2016年 wukeng. All rights reserved.
//

#import "AddViewController.h"
#import "NetManager.h"


@interface AddViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDate *date = [NSDate date];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [f stringFromDate:date];
    self.timeLabel.text = dateStr;
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem = right;
}


//上传到服务器
-(void)save{
    [NetManager AddDataWithContent:self.textView.text
                          WithData:self.timeLabel.text
                  WithSuccessBlock:^(NSData *data) {
                      //成功的话就切换到前面的表格
                      [self.navigationController popViewControllerAnimated:YES];
                      NSLog(@"11");
                  }
                     WithFailBlock:^(NSError *error) {
                         //不成功的话把错误信息显示到textView上
                         NSString *failStr = [error localizedDescription];
                         self.textView.text = failStr;
                     }];
}

//-(void) upLoadFile{
//    // 请求的网址
//    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=393213639@qq.com&type=JSON&"];
//    //请求
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    //设置为POST
//    request.HTTPMethod = @"POST";
//    //设置请求体
//    NSString *bodyStr = [NSString stringWithFormat:@"action=add&content=%@&date=%@",self.textView.text,self.timeLabel.text];
//    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//    //发送请求
//    [NSURLConnection sendAsynchronousRequest:request
//                                       queue:[NSOperationQueue mainQueue]
//                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//                               if (connectionError == nil) {
//                                   //成功的话就切换到前面的表格
//                                   [self dismissViewControllerAnimated:YES completion:nil];
//                               }
//                               else{
//                                   //不成功的话把错误信息显示到textView上
//                                   NSString *failStr = [connectionError localizedDescription];
//                                   self.textView.text = failStr;
//                               }
//                           }];
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
